//
//  SecurityTaskDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/17.
//

import Foundation
import UIKit

class LzglSaveParam: Encodable {
    var id: Int64?
    var rwid: Int64?
    var batched: Bool?
    var lng: String?
    var lat: String?
    var location: String?
    var lzrmc: String?
    var lzrhm: String?
    var rwjlMbList: [RwjlUploadParam]?
    var mode: String?

    init(id: Int64? = nil, rwid: Int64? = nil, batched: Bool? = nil, lng: String? = nil, lat: String? = nil, location: String? = nil, lzrmc: String? = nil, lzrhm: String? = nil, rwjlMbList: [RwjlUploadParam]? = nil, mode: String? = nil) {
        self.id = id
        self.rwid = rwid
        self.batched = batched
        self.lng = lng
        self.lat = lat
        self.location = location
        self.lzrmc = lzrmc
        self.lzrhm = lzrhm
        self.rwjlMbList = rwjlMbList
        self.mode = mode
    }
}

class RwjlUploadParam: Encodable {
    var id: Int64
    var rwid: Int64
    var answers: String // 0,1
    var images: [[String: String]]?
    var remark: String?

    init(id: Int64, rwid: Int64, answers: String, images: [[String : String]]? = nil, remark: String? = nil) {
        self.id = id
        self.rwid = rwid
        self.answers = answers
        self.images = images
        self.remark = remark
    }
}

class RwjlMbListParam {
    var id: Int64
    var rwid: Int64
    var answers: String // 0,1
    var images: [UIImage]?
    var remark: String?
    
    init(id: Int64, rwid: Int64, answers: String, images: [UIImage]? = nil, remark: String? = nil) {
        self.id = id
        self.rwid = rwid
        self.answers = answers
        self.images = images
        self.remark = remark
    }
}

class SecurityTaskDetailView: UIScrollView {
    var listParams: [RwjlMbListParam] = []

    let saveBtn = UIButton.createDefaultLarge("保存")
    let submitBtn = UIButton.createPrimaryLarge("一键提交")
    let titleL = UILabel()
    let contentV = UIView()
    let locV = BSLabel(withModal: locLM)

    override init(frame: CGRect) {
        super.init(frame: frame)
        keyboardDismissMode = .interactive
        showsVerticalScrollIndicator = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func checkValues(withItem item: SBoscLzglRwjlModal, isCheck: Bool?, imgs: [UIImage]?, remark: String? ) {
        var index = -1
        for (i, listParam) in listParams.enumerated() {
            if listParam.id == item.id { index = i }
        }
        if index >= 0 { listParams.remove(at: index) }
        
        if isCheck != nil {
            let rwModal = RwjlMbListParam(id: item.id ?? 0, rwid: item.rwid ?? 0, answers: isCheck == true ? "1" : "0", images: imgs, remark: remark)
            listParams.append(rwModal)
        }
    }
    
    func reload(withModal modal: LzglbaListModel?) {
        titleL.text = modal?.rwmc
        
        reload(withLists: modal?.rwjlMbDqList ?? [])
    }
    
    func updateLoc(withModal modal: LocModal?) {
        locV.value = modal?.location
    }
    
    func reload(withLists dqlists: [SRwjlMbDqModal?]) {
        contentV.removeAllSubViews()
        
        var lastDqV: UIView?
        for (i, dqlist) in dqlists.enumerated() {
            let dqV = UIView()
            contentV.addSubview(dqV)
            dqV.snp.makeConstraints { (make) -> Void in
                if lastDqV == nil {
                    make.top.equalToSuperview()
                } else {
                    make.top.equalTo(lastDqV!.snp.bottom).offset(38)
                }
                make.left.right.equalToSuperview()
                if i == dqlists.count - 1 {
                    make.bottom.equalToSuperview()
                }
            }
            
            let listTitleL = UILabel()
            listTitleL.font = UIFont.systemFont(ofSize: 16, weight: .bold)
            listTitleL.textColor = .black
            listTitleL.text = dqlist?.dqmc
            listTitleL.textAlignment = .center
            dqV.addSubview(listTitleL)
            listTitleL.snp.makeConstraints { (make) -> Void in
                make.top.equalToSuperview()
                make.left.equalToSuperview().offset(10)
            }
            
            let rwLists = dqlist?.boscLzglRwjlMbs ?? []
            
            var lastS: SceneCheckEffect?
            
            for (j, rwlist) in rwLists.enumerated() {
                let checkTF = SceneCheckEffect()
                checkTF.title = rwlist.rwmc ?? ""
                checkTF.type = SCheckEffectEnum(rawValue: rwlist.xxlxdm ?? "")
                checkTF.uploadType = UploadType(rawValue: rwlist.xxlxpz ?? "")
                if let answers = rwlist.answers {
                    checkTF.checkR.isCheck = (answers == "1") ? true : false
                    checkTF.remark = rwlist.remark
                    checkTF.imgPicker.urls = rwlist.images ?? []
                }
                checkTF.didSelectCheckWith = {(isCheck, imgs, remark) -> () in
                    self.checkValues(withItem: rwlist, isCheck: isCheck, imgs: imgs, remark: remark)
                }
                dqV.addSubview(checkTF)
                checkTF.snp.makeConstraints { (make) -> Void in
                    make.centerX.left.right.equalToSuperview()
                    make.width.equalTo(ScreenWidth - 20).priority(500)
                    if lastS == nil {
                        make.top.equalTo(listTitleL.snp.bottom).offset(18)
                    } else {
                        make.top.equalTo(lastS!.snp.bottom).offset(10)
                    }
                    if j == rwLists.count - 1 {
                        make.bottom.equalToSuperview()
                    }
                }
                lastS = checkTF
            }
            lastDqV = dqV
        }
    }
    
    func setupUI() {
        titleL.font = UIFont.systemFont(ofSize: 18)
        titleL.textColor = .hex("#306EC8")
        titleL.text = "巡检任务"
        addSubview(titleL)
        titleL.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top).offset(20)
            make.left.equalTo(self.snp.left).offset(10)
        }
        
        let desL = UILabel()
        desL.font = UIFont.systemFont(ofSize: 17)
        desL.textColor = .pmB3
        desL.text = "请巡检人员按要求认真检查"
        desL.textAlignment = .center
        addSubview(desL)
        desL.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(titleL.snp.bottom).offset(13)
            make.left.equalTo(self.snp.left).offset(10)
        }
        
        addSubview(contentV)
        contentV.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(desL.snp.bottom).offset(38)
            make.left.right.equalToSuperview()
        }
        
        let subTitle2L = UILabel()
        subTitle2L.font = UIFont.systemFont(ofSize: 16)
        subTitle2L.textColor = .black
        subTitle2L.text = "巡检人员信息"
        subTitle2L.textAlignment = .center
        addSubview(subTitle2L)
        subTitle2L.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(contentV.snp.bottom).offset(38)
            make.left.equalTo(self.snp.left).offset(10)
        }
        
        let nameV = BSLabel(withModal: nameLM)
        nameV.titleL.text = BSUser.currentUser.nickName
        addSubview(nameV)
        nameV.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(subTitle2L.snp.bottom).offset(18)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.width.equalTo(ScreenWidth - 20)
            make.height.equalTo(46)
        }
        
        let phoneV = BSLabel(withModal: phoneLM)
        phoneV.titleL.text = BSUser.currentUser.phone
        addSubview(phoneV)
        phoneV.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(nameV.snp.bottom).offset(10)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.width.equalTo(ScreenWidth - 20)
            make.height.equalTo(46)
        }
        
        addSubview(locV)
        locV.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(phoneV.snp.bottom).offset(10)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.width.equalTo(ScreenWidth - 20)
//            make.height.equalTo(46)
        }
        
        let remindV = UIView()
        remindV.backgroundColor = .hex("#F8F8F8")
        remindV.layer.cornerRadius = 10
        addSubview(remindV)
        remindV.snp.makeConstraints { make in
            make.top.equalTo(locV.snp.bottom).offset(31)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        let tipsL = UILabel()
        tipsL.font = UIFont.systemFont(ofSize: 14)
        tipsL.textColor = .black
        tipsL.text = "提交后，设备信息将发生以下变更："
        addSubview(tipsL)
        tipsL.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(remindV.snp.left).offset(18)
            make.top.equalTo(remindV.snp.top).offset(20)
        }
        
        let statusL = UILabel()
        statusL.font = UIFont.systemFont(ofSize: 14)
        statusL.textColor = .hex("#777777")
        statusL.text = "检查状态：检查中"
        addSubview(statusL)
        statusL.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(remindV.snp.left).offset(18)
            make.top.equalTo(tipsL.snp.bottom).offset(24)
        }
        
        let dateL = UILabel()
        dateL.font = UIFont.systemFont(ofSize: 14)
        dateL.textColor = .hex("#777777")
        dateL.text = "更新时间：\(Date().elTodayDateCN())"
        addSubview(dateL)
        dateL.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(remindV.snp.left).offset(18)
            make.top.equalTo(statusL.snp.bottom).offset(10)
            make.bottom.equalTo(remindV.snp.bottom).offset(-18)
        }
    
//        addSubview(submitBtn)
//        submitBtn.snp.makeConstraints { (make) -> Void in
//            make.top.equalTo(remindV.snp.bottom).offset(26)
//            make.height.equalTo(50)
//            make.centerX.equalTo(self.snp.centerX)
//            make.width.equalTo(ScreenWidth - 20)
//        }
        addSubview(saveBtn)
        saveBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(remindV.snp.bottom).offset(26)
            make.left.equalToSuperview().offset(10)
            make.height.equalTo(50)
            make.width.equalTo(90)
        }
        
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(saveBtn.snp.height)
            make.centerY.equalTo(saveBtn.snp.centerY)
            make.left.equalTo(saveBtn.snp.right).offset(10)
            make.width.equalTo(ScreenWidth - 120)
        }
        
        let bottomL = UILabel()
        bottomL.font = UIFont.systemFont(ofSize: 14)
        bottomL.textColor = .hex("#AFAEAE")
        bottomL.textAlignment = .center
        bottomL.text = "提交即授权该表单收集你填写的信息"
        addSubview(bottomL)
        bottomL.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp.centerX)
            make.top.equalTo(submitBtn.snp.bottom).offset(20)
            make.bottom.equalTo(self.snp.bottom).offset(-16)
            make.width.equalTo(ScreenWidth - 20)
        }
    }
}
